package com.ruanko.dao.impl;


import java.util.ArrayList;
import java.util.List;
import com.DBUtil.SQLHelper;
import com.ruanko.dao.UploadFileDao;
import com.ruanko.entity.UploadFileBean;

public class UploadFileDaoImpl implements UploadFileDao{
	
	private SQLHelper sqlHelper = new SQLHelper();
	private ArrayList<UploadFileBean> uploadFileList = new ArrayList<UploadFileBean>();
	/**
	 * 插入上传的文件记录
	 * @param uploadFileList
	 * @return
	 */
	public boolean insert(List<UploadFileBean> uploadFileList) {
		boolean b = false;
		try {
			System.err.println("************8\n"+uploadFileList.size()+"\n***********888");
			String sql = "insert into upload_file (uploadTime,bFilename,aFilename,userid) values(?,?,?,?)";
			String[] params = new String[uploadFileList.size()*4];
			int parmLenght = 0;
			if(!uploadFileList.isEmpty()){
				for (int i = 0; i < uploadFileList.size(); i++) {
					
						if (i != 0) {
							sql = sql + ",(?,?,?,?)";
						}
						
						params[parmLenght++] = uploadFileList.get(i).getUploadTime();
						params[parmLenght++] = uploadFileList.get(i).getbFilename();
						params[parmLenght++] = uploadFileList.get(i).getaFilename();
						params[parmLenght++] = uploadFileList.get(i).getUserID();
				}
				b = sqlHelper.executeUpdate(sql, params);
			}	
		} catch (Exception e) {
			// TODO: handle exception
		}
		return b;
	}
	/**
	 * 查询用户上传的文件记录（上传时间+文件名）
	 * @param userid
	 * @return
	 */
	public List<UploadFileBean> getUserUploadFileRecord(String userid) {
		if(uploadFileList!=null){
			uploadFileList.clear();
    	}
		String sql = "select userid,uploadTime,bFilename from upload_file where userid=?  order BY uploadTime DESC";
		String[] params = new String[1];
		params[0] = userid;
		try {
			ArrayList<Object> templist = sqlHelper.executeQuery(sql, params);
			if (!templist.isEmpty()) {
				for (int i = 0; i < templist.size(); i++) {
					Object[] objects = (Object[]) templist.get(i);
					UploadFileBean uploadFileBean = new UploadFileBean();
					uploadFileBean.setUserID(objects[0].toString());
					uploadFileBean.setUploadTime(objects[1].toString());
					uploadFileBean.setbFilename(objects[2].toString());
					uploadFileList.add(uploadFileBean);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return uploadFileList;
	}
}
